import React from 'react'
import { View, Text } from '@tarojs/components'

// 处理错误边界的高阶组件
export default function ErrorBoundary(WrappedComponent: any, source: string) {
  return class ErrorBoundary extends React.Component {
    state: {
      hasError: boolean
      error: any
    } = { hasError: false, error: null }

    static getDerivedStateFromError(error: any) {
      return { hasError: true, error: error }
    }

    render() {
      if (this.state.hasError) {
        return (
          <View>
            <View>Error from &lt;{source}/&gt; component：</View>
            <Text> {this.state.error.message} </Text>
          </View>
        )
      }

      return <WrappedComponent {...this.props} />
    }
  }
}
